﻿<%@ WebHandler Language="C#" Class="GetNewsFromSection" %>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Xml;
using BusinessLogic.ClassFactory;
using LitJson;
using RichECMS;

public class GetNewsFromSection : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
        Hashtable result = new Hashtable();
        string count = HttpContext.Current.Request["count"] ?? "20";
        string strSectionID = "";
        if (HttpContext.Current.Request["sid"] == null)
        {
            result["error"] = "参数缺失";
            result["list"] = "";
            context.Response.Write(JsonMapper.ToJson(result));
            context.Response.End();
        }
        else
        {
            if (!ValidateCommon.ParamAuth.IsNumber(HttpContext.Current.Request["sid"]))
            {
                result["error"] = "参数类型错误";
                result["list"] = "";
                context.Response.Write(JsonMapper.ToJson(result));
                context.Response.End();
            }
            else
            {
                strSectionID = HttpContext.Current.Request["sid"];
            }
        }

        bool ContainChildSection = true;
        bool bolFilterSame = false;
        int showcount = 0;
        if (IsNum(count))
        {
            showcount = int.Parse(count);
        }

        StringBuilder sbWhere = new StringBuilder();
        List<Hashtable> reslist = new List<System.Collections.Hashtable>();

        try
        {
            using (ArtSecRelationFactory objFactory = new ArtSecRelationFactory())
            {
                StringBuilder strWhere = new StringBuilder();
                SectionsFactory objSCFactory = new SectionsFactory();
                BusinessLogic.BusinessRules.Sections objSections = objSCFactory.NewInstance();
                if (strSectionID != "")
                {
                    string strSTid = strSectionID;
                    if (strSectionID.IndexOf(',') == -1 && ContainChildSection == true)
                    {
                        string stridtemp = strSectionID;
                        GetSectionFromParentId(ref strSTid, Convert.ToInt32(strSTid));
                    }

                    strWhere.Append("select Article.chvTitle,Article.dtmInTime,Article.chvSummary,Article.chvArticleSource,Article.chvDescription,Article.txtContent,Article.chvArticleInMan,Article.chvSubTitle,Article.chvMainMiniPic,Article.txtContent,ArtSecRelation.*  from ArtSecRelation,Article   where 1=1 and Article.intArticleId=ArtSecRelation.intArticleId and  ArtSecRelation.intSectionID in (" + strSTid + ") " + sbWhere + " order by ArtSecRelation.intSort desc, Article.dtmInTime desc,ArtSecRelation.intSecArtID desc");
                }
                else
                {
                    strWhere.Append("select Article.chvTitle,Article.dtmInTime,Article.chvSummary,Article.chvArticleSource,Article.chvDescription,Article.txtContent,Article.chvArticleInMan,Article.chvSubTitle,Article.chvMainMiniPic,Article.txtContent,ArtSecRelation.*  from ArtSecRelation,Article   where 1=1 and Article.intArticleId=ArtSecRelation.intArticleId " + sbWhere + " order by ArtSecRelation.intSort desc, Article.dtmInTime desc,ArtSecRelation.intSecArtID desc");
                }
                /*
                strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.* from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID and Article.intState = '" + Request["type"].ToString().Trim().Replace("'","")+ "' " + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Article.intSort desc ,Article.dtmInTime desc,Article.intArticleID desc)t order by intSort asc, dtmInTime asc ,intArticleID asc)m " + " order by intSort asc, dtmInTime asc ,intArticleID asc)mm  order by intSort desc, dtmInTime desc ,intArticleID desc");
                */
                BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objFactory.CreatorByFullSql(strWhere.ToString());
                if (objArtSecRelation != null)
                {
                    ArrayList objReturnArrayFiltersame = new ArrayList();
                    System.Data.DataTable tblTmp = objArtSecRelation.Data.Tables[0];
                    System.Data.DataTable tblResult = tblTmp.Clone();
                    foreach (System.Data.DataRow objRow in tblTmp.Rows)
                    {
                        if (bolFilterSame == true)
                        {
                            if (objReturnArrayFiltersame.Contains(objRow["intArticleId"]))
                            {
                                continue;
                            }
                            else
                            {
                                System.Data.DataRow objRowtemp = tblResult.NewRow();
                                objRowtemp.ItemArray = (object[])objRow.ItemArray.Clone();
                                tblResult.Rows.Add(objRowtemp);
                            }
                            objReturnArrayFiltersame.Add(objRow["intArticleId"]);
                        }
                        else
                        {
                            System.Data.DataRow objRowtemp = tblResult.NewRow();
                            objRowtemp.ItemArray = (object[])objRow.ItemArray.Clone();
                            tblResult.Rows.Add(objRowtemp);
                        }
                    }

                    for (int i = 0; i < tblResult.Rows.Count; i++)
                    {
                        if (i >= 0 && i < showcount)
                        {
                            Hashtable temp = new Hashtable();
                            temp["aid"] = tblResult.Rows[i]["intSecArtID"];
                            temp["atitle"] = tblResult.Rows[i]["chvTitle"];
                            temp["aimg"] = tblResult.Rows[i]["chvMainMiniPic"];
                            ////temp["info"] = tblResult.Rows[i]["chvSummary"];
                            temp["chvsubtitle"] = tblResult.Rows[i]["chvSubTitle"];
                            ////temp["content"] = tblResult.Rows[i]["chvDescription"];
                            ////temp["answer"] = tblResult.Rows[i]["txtContent"];
                            temp["atime"] = Convert.ToDateTime(tblResult.Rows[i]["dtmInTime"]).ToString("yyyy-MM-dd");
                            temp["asource"] = tblResult.Rows[i]["chvArticleSource"];
                            temp["astate"] = tblResult.Rows[i]["intType"];
                            temp["acontent"] = tblResult.Rows[i]["txtContent"];
                            temp["aArticleInMan"] = tblResult.Rows[i]["chvArticleInMan"];
                            reslist.Add(temp);
                        }
                    }

                }
                objSCFactory.Dispose();
            }

        }
        catch (Exception error)
        {
            result["error"] = error.Message.Replace("/r/n", "");
        }


        result["list"] = reslist;
        context.Response.Write(
           JsonMapper.ToJson(result));
    }

    /// <summary>
    /// 检测是否能转化为数字
    /// </summary>
    /// <param name="param">检测字符串</param>
    /// <returns>输出true/false</returns>
    public static bool IsNum(string param)
    {
        const string regEx = @"^[0-9]*$";
        if (System.Text.RegularExpressions.Regex.IsMatch(param, regEx))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    /// <summary>
    /// 根据父ID获取子栏目
    /// </summary>
    private void GetSectionFromParentId(ref string strSTIDlist, int intParentId)
    {
        try
        {
            using (SectionsFactory objFactory = new SectionsFactory())
            {
                BusinessLogic.BusinessRules.Sections objsection = objFactory.CreatorBySql("intParentID =" + intParentId.ToString());
                if (objsection != null)
                {
                    BusinessLogic.BusinessRules.Sections objsectionTemp;
                    for (int i = 0; i < objsection.Count; i++)
                    {
                        strSTIDlist = strSTIDlist + "," + objsection.DataItem.intSectionID;
                        objsectionTemp = objFactory.CreatorBySql("intParentID =" + objsection.DataItem.intSectionID.ToString());
                        if (objsectionTemp != null)
                        {
                            for (int j = 0; j < objsectionTemp.Count; j++)
                            {
                                strSTIDlist = strSTIDlist + "," + objsectionTemp.DataItem.intSectionID;
                                GetSectionFromParentId(ref strSTIDlist, objsectionTemp.DataItem.intSectionID);
                                objsectionTemp.SelectRow(j + 1);
                            }
                        }
                        objsection.SelectRow(i + 1);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("获取子栏目ID是发生异常：" + ex.Message.Replace("\r\n", ""));
        }
    }
}